package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags={"部门接口"})
public class DepartmentController {

    //注入业务层依赖
    @Autowired
    private IDepartmentService service;

    /**
     * 查询所有的方法
     */
    @GetMapping("/treeList")
    private AjaxResult treeList(){
        //1.查询所有
        List<Department> list = service.treeList();
        //2.封装对象返回
        return AjaxResult.createSuccess(list);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public AjaxResult findAll(){
        List<Department> list = service.findAll();
        return AjaxResult.createSuccess(list);
    }
    /**
     * 分页查询所有
     */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    private PageList<Department> getAllDeptByPage(@RequestBody DepartmentQuery query){

        return service.findPage(query);
    }
    /**
     * 查询所有的上级部门们
     */
    @PostMapping("/findParentDepts")
    private AjaxResult findParentDepts(){

        return AjaxResult.createSuccess(service.findParentDepts());
    }

    /**
     * 查询一个的方法
     */
    @GetMapping("/findOne/{id}")
    private AjaxResult findOne(@PathVariable("id") Long id){

        return AjaxResult.createSuccess(service.findOne(id));
    }

    /**
     * 修改和新增联合方法
     * @param department
     * @return
     */
    @PostMapping
    @ApiOperation(value="新增/修改部门", notes="大飞峰新增/修改成功")
    public AjaxResult saveOrUpdate(@Valid @ApiParam(value = "新增/修改部门参数", required = true) @RequestBody Department department){
        //调用service层
        try {
            service.saveOrUpdate(department);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("系统异常,请联系管理员");
        }
    }

    /**
     * 删除方法
     * @param id 主键id
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        //调用删除方法
        try {
            service.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("系统异常,请联系管理员");
        }
    }
    /**
     * 批量删除的方法
     */
    @RequestMapping(value = "/deleteBatch",method = RequestMethod.POST)
    public AjaxResult batchDelete(@RequestBody DepartmentQuery query){
        try {
            service.batchDelete(query.getIds());
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作失败");
        }
    }

}
